package com.xsherl.leetcode.base;

import com.xsherl.leetcode.utils.PrintUtils;

import java.util.*;

public class TreeNode {

    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static TreeNode buildTree(Integer... vals) {
        if (vals.length == 0){
            return null;
        }
        LinkedList<TreeNode> deque = new LinkedList<>();
        TreeNode root = new TreeNode(vals[0]);
        deque.offer(root);
        int i = 1;
        while (i < vals.length){
            TreeNode node = deque.poll();
            if (vals[i] != null){
                node.left = new TreeNode(vals[i]);
                deque.offer(node.left);
            }
            i++;
            if (i < vals.length && vals[i] != null){
                node.right = new TreeNode(vals[i]);
                deque.offer(node.right);
            }
            i++;
        }
        return root;
    }

    @Override
    public String toString() {
        return "["+ val+ "]";
    }
}
